import { defineEventHandler } from 'h3'
import songs from '~/server/data/songs.json'
import type { Song } from '~/types/song'

export default defineEventHandler(() => {
  const allSongs = songs as Song[]
  if (allSongs.length === 0) {
    throw createError({
      statusCode: 404,
      statusMessage: 'No songs found',
    })
  }

  const randomIndex = Math.floor(Math.random() * allSongs.length)
  return allSongs[randomIndex]
})